﻿Imports System.IO
Public Class ClsFile
    'represents general file

#Region "Attributes"
#Region "Private attributes"
    Protected MeStateOfFile As GeFileState = GeFileState.kkNotInit  'state of file
    Protected MsPath As String = "" 'path to file with filename
#End Region
#End Region
#Region "Constructors"
    Sub New()

    End Sub

    Sub New(ByVal IsPath As String)
        Init(IsPath)
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    Public ReadOnly Property StateOfFile() As GeFileState
        Get
            Return MeStateOfFile
        End Get
    End Property
    Public ReadOnly Property Path() As String
        Get
            Return MsPath
        End Get
    End Property
#End Region
#End Region
#Region "Functions"
    'initialization of file
    Public Overridable Function Init(ByVal IsPath As String) As GeActionResult
        Try
            If MeStateOfFile = GeFileState.kkNotInit Then
                MsPath = IsPath 'set path in all cases!
                If IO.File.Exists(IsPath) Then
                    MeStateOfFile = GeFileState.kkReady
                    Return GeActionResult.kkOk
                Else
                    Debug.Print("CHYBA!!! - ClsFile.Init - If IO.File.Exists(IsPath)")
                    Throw New Exception
                End If
            Else
                Debug.Print("CHYBA!!! - ClsFile.Init - If MeStateOfFile = GeFileState.kkNotInit")
                Throw New Exception
            End If
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
    'open file
    Public Overridable Function OpenFile() As GeActionResult
        Try
            If MeStateOfFile = GeFileState.kkReady Then
                Return GeActionResult.kkOk
            Else
                Debug.Print("CHYBA!!! - ClsFile.OpenFile")
                Throw New Exception
            End If
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
    'close file
    Public Overridable Function CloseFile() As GeActionResult
        Try
            If (MeStateOfFile = GeFileState.kkNewFile) Or (MeStateOfFile = GeFileState.kkOpened) Then
                Return GeActionResult.kkOk
            Else
                Debug.Print("CHYBA!!! - ClsFile.CloseFile")
                Throw New Exception
            End If
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
#End Region

End Class
